package van.planifolia.demo;

/**
 * Created by Intellij IDEA<br>
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/10/31 17:58
 */
public class BankingBusiness {
    public static void main(String[] args) {
        // 创建银行账号实例
        Bank bank = new Bank();
        // 模拟小明存钱
        new Thread(() -> {
            for (int i = 0; i < 20; i++) {
                bank.save(1000);
                System.out.println(Thread.currentThread().getName() + "存了1000元，现在余钱为->" + bank.getDeposit());
            }
        }, "小明").start();
        // 模拟小红取钱
        new Thread(() -> {
            for (int i = 0; i < 20; i++) {
                if (bank.getDeposit() <= 0){
                   i--;
                    System.out.println("余额不足");
                    continue;
                }
                    bank.take(1000);
                System.out.println(Thread.currentThread().getName() + "取了1000元，现在余钱为->" + bank.getDeposit());
            }
        }, "小红").start();
    }

}
class Bank {
    // 余钱
    private double deposit = 0;

    // 存款方法
    public void save(double saveMoney) {
        deposit = deposit + saveMoney;
    }

    // 取款方法
    public void take(double takeMoney) {
        deposit = deposit - takeMoney;
    }

    public double getDeposit() {
        return this.deposit;
    }
}
